package com.mmall.common.spring.service.impl;

import com.common.framework.util.BooleanUtils;
import com.common.framework.util.CacheUtils;
import com.mmall.common.spring.service.RedisService;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;


@Service("redisService")
public class LocalRedisServiceImpl implements RedisService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * String（字符串）操作类
     */
    @Resource(name = "redisTemplate")
    private ValueOperations<String, Object> valueOps;

    /**
     * List（列表）操作类
     */
    @Resource(name = "redisTemplate")
    private ListOperations<String, Object> listOps;

    /**
     * Set（集合）操作类
     */
    @Resource(name = "redisTemplate")
    private SetOperations<String, Object> setOps;

    /**
     * SortedSet（有序集合）操作类
     */
    @Resource(name = "redisTemplate")
    private ZSetOperations<String, Object> zSetOps;

    /**
     * Hash（哈希表）操作类
     */
    @Resource(name = "redisTemplate")
    private HashOperations<String, String, Object> hashOps;

    @Override
    public Object vGet(CacheUtils.CacheName cacheName, String key) {
        if (cacheName.getType() != CacheUtils.CacheType.V) {
            throw new IllegalArgumentException("expected V found " + cacheName.getType());
        }
        return valueOps.get(cacheName.getPrefix().concat(key));
    }

    @Override
    public void vPut(CacheUtils.CacheName cacheName, String key, Object value) {
        if (cacheName.getType() != CacheUtils.CacheType.V) {
            throw new IllegalArgumentException("expected V found " + cacheName.getType());
        }
        if (cacheName.getExpiration() > 0) {
            valueOps.set(cacheName.getPrefix().concat(key), value, cacheName.getExpiration(), TimeUnit.SECONDS);
        } else {
            valueOps.set(cacheName.getPrefix().concat(key), value);
        }
    }

    @Override
    public boolean vPutIfAbsent(CacheUtils.CacheName cacheName, String key, Object value) {
        if (cacheName.getType() != CacheUtils.CacheType.V) {
            throw new IllegalArgumentException("expected V found " + cacheName.getType());
        }
        if (cacheName.getExpiration() > 0) {
            Boolean saveSuccess = valueOps.setIfAbsent(cacheName.getPrefix().concat(key), value, cacheName.getExpiration(), TimeUnit.SECONDS);
            return BooleanUtils.isTrue(saveSuccess);
        }
        Boolean saveSuccess = valueOps.setIfAbsent(cacheName.getPrefix().concat(key), value);
        return BooleanUtils.isTrue(saveSuccess);
    }

    @Override
    public Long vIncr(CacheUtils.CacheName cacheName, String key, long incr) {
        if (cacheName.getType() != CacheUtils.CacheType.V) {
            throw new IllegalArgumentException("expected V found " + cacheName.getType());
        }
        if (incr == 0) {
            throw new IllegalArgumentException("自增步长不能为 0");
        }
        return valueOps.increment(cacheName.getPrefix().concat(key), incr);
    }
}
